package com.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

// 所有配置，可以和 "alipay" 绑定
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "9021000141622150";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCcQr1iGwiukdkrv5c7MvpQ4EUola5efGsSDbuvc2kJ5ZBSEmTNvz/VS0Dzlrw9uQwhnUdihCZfrgOD2MQVuiHXA0Va4m+xcBIdzFCK08PCOyOmMPCe5uDqzsmCdYRMYcXOVSUQ7Ve+/kSIYGQQg6K4uNaF/E2pqH4XjNJfeeCPIveMByjPJFFJaVma4GWdnKZK+C9eBKdlgzNWEC61zSn0MWOA76rVpUEwYEblGGc5M8r7yg86QJlrjYyl2ZHScw9imaHZgHxE6q5ZH/HYsEF0IaQwMs1bFt2MF19VWwn4GzfF1miUxXDpYOAvu39ggqvMXJILrtl125f6uezGoNGTAgMBAAECggEAdcrNrnXIjqf9OMBdhU/BaFQXbiefYTSTFfHqSLFMPhyiNWwONxbzTJtE5Br93d1vqqtAE6CguMGZfrtZ3TMJrWaW0pMdNXnQ/kt1b6GA0Rr5nZ8I1BAg/oBRYRmrWFTf4tFGdsMjwSM1kERxSg2hDXSwqiHufXU5UBFkyHo8cEjxbLrn1ahcSbE8S97RxxZ/W/SRn/TTU6XP+jwe77g8RJbdT1uzNbct7RZNjJuHMYApiyi5dlMyQD1mCmeF2ziP7s7vmKJnNvGiLpAhga91c2VyXl/driJZXrOxDFqwnDr8uGXJqYkEgxJqwyktXRUv5CPilu2XXwVqITTM+heUyQKBgQDeWUTpYhyZ5EUQGrWIRhpUxhJrQxXnoOYHLdR/5krkTwFDikxOTZBA9vFrKcwFWospzkfUGNpY/uQr3fy/RLwYzSbmazeCRAJeFvIkbhxwxxsNZ1ScdLGC9OFCy8T69GgNAw0NQe4CdIBAxp2voOVD3r9+/RXrhWczz3/je2pbNwKBgQCz6PD5lj7opgQc6PxZSZl3/f+MSzG5K0bG7yooKRTx6uDXvhabx4LlCfNLFdiS3TyuPVaDdB6RXkNVznjDrYDZH3fO7As9lBrA0W9w2StJJRB3hKW6o6ni/ijHodqurxJWaTXIKsmN3lwneSOczIpwwaLdLpwr9+GifO38eB8ChQKBgHSCaHa2n3AQShQfOsJWoZOMWrhz5bWC/SYD4NbIMLQjpfDjNgrN3+n6vBRASZUAle1pFwBPqZJ6tcs6vibneHyye1DAG80Zn/f3pBhszmBWJJnIbHCzQRTzBlp+9g/eI+u1jpBE3NfSfiXzLUuLTrdasBN1wJeyRprCfywPTnLPAoGATr4JmnPUHOdsrWqb6Eh6TzZwfur6XF1AoRlUjjdM34U527NrGct80tpEDf0FyWHjiS93ihlst5tWRVwVJy/tURqgyYQud0k3XGVOLUof0+fRCT6ZetLfLGapWpbQq9wcXo216GIxzVIha3VtzNcJUMivi7c2y1e2/fNIBlRc3jECgYEAgcNYKgliC9SA71V12xk8+Dq3A7VrzERMSuJ9zRfjaTC68f7+zaCz+twsXeSipp4Hik2H1C1HwyyBFtUivV1iIDSewOzif8Y/cvMjGwwXg8Q8S+d/8e8JrD8IkIijJaiTZe2HRJJLTDder5kGqKTUYVeZZ0Tm90UkEoQUGnmiCt4=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlxzCOtsNr63H/7mJs17a7UN/kjjhy2NXdoHFx6mG1PH30Ot0MlNSi6EXDZ3rJlGBf22cNKaGdreQe3mkrYMmwsqgxxUjvtF0z0/BMxbhLfmux6Zomcejl0msiJ0FC7Y9bfF34RvRFuNr7NyS5kJJ2/sDY9NnrgV+HG6NS2VjoMQhRr959aCx+V2HzhbSjsibhTVD8DipdUhA+sqePQBui0uJ+Iem9V+LxbwiDwGmux+fiSqSA7BVGdbM0CLeZAjXF5aovVfX/uJBdCiv5zjUTAYr/NfmQ/qiLgiCwLX5HckbRjpXOq7raeRmVSJokOAizwwtZx5+Nk0Gg4plcKD0TQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://ugfbz3.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
